package com.hpeng.tml.action;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Locale;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.Workbook;
import jxl.WorkbookSettings;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.hpeng.daily.service.AssetCardManager;
import com.hpeng.sys.model.AssetCard;
import com.hpeng.sys.service.Function;
import com.hpeng.sys.service.LoginUserMgr;
import com.opensymphony.xwork2.ActionSupport;

@Component("assetCardOutputcheckDepExcelAction")
@Scope("prototype")
public class AssetCardOutputcheckDepExcelAction extends ActionSupport implements
		ServletResponseAware, ServletRequestAware {
	private static final long serialVersionUID = 1L;
	protected HttpServletRequest request = null;
	protected HttpServletResponse response = null;
	@Resource
	private AssetCardManager assetCardManager;
	@Resource
	private LoginUserMgr loginUserMgr;
	@Resource
	private Function function;
	
	private String msg;
	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public void setServletResponse(HttpServletResponse resp) {
		this.response = resp;
	}

	public void setServletRequest(HttpServletRequest req) {
		this.request = req;
	}

	// 获取输出流
	public InputStream getInputExcel() {
		return (InputStream) request.getAttribute("excelStream");
	}

	@Override
	public String execute() {
		String userDepName=loginUserMgr.getUserDeptName(request);
		String dsname="";
		try {
			dsname = new String(request.getParameter("dsname").getBytes("iso8859-1"),"gb2312");
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
		System.out.println("depTree:dsname-->"+dsname+",userDepName-->"+userDepName);
		//if(userDepName.equals(dsname)){
			List<AssetCard> list = assetCardManager.getAllAssetCardByCondition(" from AssetCard where depname='"+dsname+"'", " order by acid asc ");
			String[] columnNames = {"资产编号","资产名称","资产状态","资产来源","资产类别ID","所在部门","申报原因","申报部门","申报价值","生产日期","厂家","预计停用日期","实际停用日期","资产数量"};
			//if (list.size() != 0) {
			if (list!= null) {
				int rows = 0;
				ByteArrayOutputStream os = new ByteArrayOutputStream();
				WritableWorkbook book;
				try {
					//避免乱码
					WorkbookSettings setting = new WorkbookSettings();
				    Locale local = new Locale("zh","CN");
				    setting.setLocale(local);
				    setting.setEncoding("UTF-8");
					Workbook.createWorkbook(os, setting);
					    
					book = Workbook.createWorkbook(os);
					WritableSheet sheet = book.createSheet("第一页", rows); //设置工作薄当前页数
					for (int i = 0; i < list.size(); i++) {
						for(int j=0; j<columnNames.length; j++){
						//Label..第一个参数为列,第二个参数为行,第三个参数为值.
							Label label1 = new Label(j, 0, columnNames[j]);
							sheet.addCell(label1);
						}
						//输出
						Label label2 = new Label(0,i+1,list.get(i).getAcid());
						Label label3 = new Label(1,i+1,list.get(i).getAcname());
						Label label4 = null;
						String statusCode = list.get(i).getAcstatus().substring(0,1);
						String subCode = list.get(i).getAcstatus().substring(1);
						label4 = new Label(2,i+1,function.getStatusDesc(statusCode, subCode));
						Label label5 = new Label(3,i+1,list.get(i).getAssetcomefrom());
						Label label6 = new Label(4,i+1,list.get(i).getCategory().getCatid());
						Label label7 = new Label(5,i+1,list.get(i).getDepname());
						Label label8 = new Label(6,i+1,list.get(i).getReportreason());
						Label label9 = new Label(7,i+1,list.get(i).getReportdepartment());
						Label label10 = new Label(8,i+1,list.get(i).getReportprice()+"");
						Label label11 = new Label(9,i+1,list.get(i).getProducer());
						Label label12 = new Label(10,i+1,list.get(i).getProducedate()+"");
						Label label13 = new Label(11,i+1,list.get(i).getPlanstopdate()+"");
						Label label14 = new Label(12,i+1,list.get(i).getActualstopdate()+"");
						Label label15 = new Label(13,i+1,list.get(i).getCount());
						sheet.addCell(label2);
						sheet.addCell(label3);
						sheet.addCell(label4);
						sheet.addCell(label5);
						sheet.addCell(label6);
						sheet.addCell(label7);
						sheet.addCell(label8);
						sheet.addCell(label9);
						sheet.addCell(label10);
						sheet.addCell(label11);
						sheet.addCell(label12);
						sheet.addCell(label13);
						sheet.addCell(label14);
						sheet.addCell(label15);
					}
					book.write();
					book.close();
					request.setAttribute("excelStream", new ByteArrayInputStream(os.toByteArray()));
					request.setAttribute("filename", "myexcel.xls");//设置文件名 
					return "success";
				} catch (IOException e) {
					e.printStackTrace();
					return "error";
				} catch (RowsExceededException e) {
					e.printStackTrace();
					return "error";
				} catch (WriteException e) {
					e.printStackTrace();
					return "error";
				}
			}else{
				msg="统计数据不存在";
				System.out.println("统计数据不存在");
				return "error";
			}
		//}else {
			//msg="导出失败，不能操作其他部门的资产";
			//System.out.println("导出失败，不能操作其他部门的资产");
			//return "error";
		//}
	}
}
